package com.ganjiang.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ganjiang.entity.UserCenter;
import com.ganjiang.service.UserCenterService;

@Controller
public class UserCenterController {

	@Resource
	private UserCenterService userCenterService;

	@RequestMapping("/userCenterlist")

	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("userCenterList", userCenterService.selectAllUserCenter());

		mav.setViewName("userCenterList");
		return mav;
	}

	@RequestMapping("/toAddUserCenter")

	public String toAdd() {
		return "userCenterAdd";
	}

	@RequestMapping("/userCenterAdd")
	public void userCenterAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {

		int uid = Integer.valueOf(request.getParameter("uid"));
		String password = request.getParameter("password");
		String myid = request.getParameter("myid");
		String myidkey = request.getParameter("myidkey");
		String regip = request.getParameter("regip");
		int regdate = Integer.valueOf(request.getParameter("regdate"));
		String lastloginip = request.getParameter("lastloginip");
		int lastlogintime = Integer.valueOf(request.getParameter("lastlogintime"));
		String salt = request.getParameter("salt");
		String secques = request.getParameter("secques");

		UserCenter userCenter = new UserCenter();

		userCenter.setUid(uid);
		userCenter.setPassword(password);
		userCenter.setMyid(myid);
		userCenter.setMyidkey(myidkey);
		userCenter.setRegip(regip);
		userCenter.setRegdate(regdate);
		userCenter.setLastloginip(lastloginip);
		userCenter.setLastlogintime(lastlogintime);
		userCenter.setSalt(salt);
		userCenter.setSecques(secques);

		userCenterService.addUserCenter(userCenter);

		response.sendRedirect("/userCenterlist");

	}

	@RequestMapping("/updateUserCenter")
	public void updateUserCenter(HttpServletRequest request, HttpServletResponse response) throws IOException {

		int uid = Integer.valueOf(request.getParameter("uid"));
		String password = request.getParameter("password");
		String myid = request.getParameter("myid");
		String myidkey = request.getParameter("myidkey");
		String regip = request.getParameter("regip");
		int regdate = Integer.valueOf(request.getParameter("regdate"));
		String lastloginip = request.getParameter("lastloginip");
		int lastlogintime = Integer.valueOf(request.getParameter("lastlogintime"));
		String salt = request.getParameter("salt");
		String secques = request.getParameter("secques");

		UserCenter userCenter = new UserCenter();

		userCenter.setUid(uid);
		userCenter.setPassword(password);
		userCenter.setMyid(myid);
		userCenter.setMyidkey(myidkey);
		userCenter.setRegip(regip);
		userCenter.setRegdate(regdate);
		userCenter.setLastloginip(lastloginip);
		userCenter.setLastlogintime(lastlogintime);
		userCenter.setSalt(salt);
		userCenter.setSecques(secques);

		userCenterService.updateCenter(userCenter);

		response.sendRedirect("/userCenterlist");

	}

	@RequestMapping("/deleteUserCenter")

	public void deleteUserCenter(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.valueOf(request.getParameter("uid"));

		userCenterService.deleteUsercenter(id);
		response.sendRedirect("userCenterlist");

	}

	@RequestMapping("/getUserCenter")
	public String findById(HttpServletRequest request, HttpServletResponse response) {

		int uid = Integer.valueOf(request.getParameter("uid"));

		UserCenter userCenter = userCenterService.findById(uid);

		request.setAttribute("useCenter", userCenter);

		return "userCenterUpdate";

	}

}
